1. Field of the Invention
The present invention relates to virtual machine technology. More particularly, the present invention relates to a method and apparatus for loading classes into virtual machines and a method and apparatus for re-organizing class archives.
2. Description of the Related Art
Java™ is a cross-platform tool, which has good portability and is platform independent. Hence, Java™ has been put into extensive use in various fields. The virtual machine is an engine for Java™ applications for interpreting compiled applications, which plays a crucial role in platform-independent characteristics. Applications written in the Java™ language are not compiled into standard executable codes but into bytecodes, that is, a binary format suitable for execution by virtual machines, and packed and saved in class archives (for example, jar files). During execution of applications, a class loader loads Java™ application bytecodes, in a class file format, from class archives into a virtual machine that is running on or embedded into application platforms. The class file is of a binary file format defined for Java™ programs and can be interpreted by any virtual machine. Each class file represents a complete description of a Java™ class or interface, which is a special abstract class. As long as there is a suitable virtual machine on an application platform, Java™ applications can be loaded to the virtual machine and then run on the application platform.
During the procedure of class loading in the prior art, the task of class loading is first delegated to a default root class loader. The root class loader can assign concrete tasks to respective child class loaders that search for class archives along the class path until they find the class archives. Then, the found class archives are unpacked, and each of the file names in an archive is matched with the desired class name one by one until the completely matched class file is found. Afterwards, entries are searched for and bytecodes of the class are extracted and loaded to the virtual machine.
However, there are several problems during the procedure of class loading in the prior art.
A first problem is code redundancy. During the procedure of class loading in the prior art, classes are loaded to virtual machines in the class file unit and an internal data structure is created for each loaded class. Additionally, all binary data in the class file are transferred to the method area within a memory without any extraction and contraction. However, in many cases, the whole class that is loaded to the virtual machines or some portions thereof is seldom or never used after loading. This is because those applications are usually developed for ordinary users and that only a small portion of applications are used for a specific case. Hence, for the specific case, this will lead to a pretty inefficient usage of memory space. As used herein, “case” means the set of a series of activities, where a specific user uses applications in a given software and hardware environment.
To obtain knowledge of the current code redundancy situation, a standard application was tested using a Java™ performance analyzer (Profiler), Java™ heap analysis tool (JHat) and an available syntactic analysis, and lexical analysis tool (Antlr). This standard application includes a number of functions; the main functions were tested. In order to obtain relatively stable results, times of tests have been implemented, and averages values of times of tests are used as test results. According to the results, methods that are used for startup share about 32.8% of all the loaded methods, bytecodes that are used for startup account for about 0.13% of all the loaded bytecodes, methods that are used for plug-in development account for about 87.2% of all the loaded methods, and bytecodes that are used for plug-in development account for about 1.3% of all the loaded bytecodes. This experiment shows that there is serious code redundancy in current class loading methods. In particular, such code redundancy will exert a more adverse effect on program performance in an environment that has limited memory resources, such as a mobile device.
Another problem in the procedure of class loading in the prior art is that class loading time is too long. This is because, on the one hand, all class files are loaded to a virtual machine regardless of whether they are likely to be used or are not used during later running. On the other hand, it takes a large amount of time to search for the proper class during the procedure of class file loading. The too long class loading time significantly affects program performance.
U.S. Pat. No. 7,020,874 B2, which was issued on Mar. 28, 2006, discloses a technique for loading class files to virtual machines. This technique loads class files to a memory portion of a computer system, selectively loads class files from the memory portion to a virtual machine, recognizes and extracts desired parts of desired classes from the memory portion when a desired class file is not in the virtual machine, and loads them to the virtual machine. The technique disclosed in this patent reduces code redundancy to a certain extent and shortens class loading time. However, this method needs a dedicated memory for loading class files, thereby occupying precious system resources and thus producing low memory utilization efficiency. In particular, program performance might be affected greatly in an environment that has limited memory resources, such as a mobile device.
Therefore, there is a need in the technical field of class loading to improve current class loading manners in order to provide more proper class loading techniques.